﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using System.Xml.Linq;
using Exentials.Xoox.Spreadsheet;

namespace Exentials.Xoox.Styles
{
    public class XStyles : XDocumentBase<WorkbookStylesPart>
    {
        private XStyleSheet _styleSheet;

        public XStyles(WorkbookStylesPart stylePart)
            : base(stylePart)
        {
            _styleSheet = new XStyleSheet();
            Document.Add(_styleSheet.GetXElement());

            XBorder border = new XBorder();

            border.Bottom.Color.Rgb = "FFFF0000";
            border.Bottom.Style = XBorderStyle.@double;

            int borderId = _styleSheet.Borders.Add(border);

            int fillId = _styleSheet.Fills.Add(new XPatternFill(PatternType.none));

            int fontId = _styleSheet.Fonts.Add(new XFont() { Size = 10, Name = "Calibri", Scheme = XFontScheme.minor });

            _styleSheet.FormattingRecords.Add(new XFormat() { NumFmtId = 0, FontId = 0, FillId = fillId, BorderId = 0 });
            _styleSheet.CellFormats.Add(new XFormat() { NumFmtId = 0, FontId = 0, FillId = fillId, BorderId = borderId, XfId = 0 });
            _styleSheet.CellStyles.Add(new XCellStyle("Normal", 0));
        }

        public void ApplyNumberFormat(XCell cell, NumberFormat numberFormat)
        {
            XFormat format = _styleSheet.CellFormats.ToList()[(int)cell.Style];

            cell.Style = _styleSheet.CellFormats.Add(new XFormat()
                {
                    NumFmtId = (int)numberFormat,
                    FontId = format.FontId,
                    FillId = format.FillId,
                    BorderId = format.BorderId,
                    XfId = format.XfId,
                    ApplyNumberFormat = true
                });

        }
    }
}
